<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采购单号" prop="purchaseOrderNo">
        <el-input
          v-model="queryParams.purchaseOrderNo"
          placeholder="请输入采购单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料号" prop="materialNo">
        <el-input
          v-model="queryParams.materialNo"
          placeholder="请输入物料号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="specification">
        <el-input
          v-model="queryParams.specification"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请日期" prop="applicationDate">
        <el-date-picker clearable
          v-model="queryParams.applicationDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择申请日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:ZcTemporaryPurchasePlan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:ZcTemporaryPurchasePlan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:ZcTemporaryPurchasePlan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:ZcTemporaryPurchasePlan:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ZcTemporaryPurchasePlanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司代码" align="center" prop="companyCode" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="图片" align="center" prop="attachment1" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.attachment1" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column  show-overflow-tooltip label="语音" width="80" align="center" prop="attachment2">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-video-play"
            @click="handlePlayMp3(scope.row)"
          >播放</el-button>
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="applicant" />
      <el-table-column label="申请日期" align="center" prop="applicationDate" width="180"/>
      <el-table-column label="审核人" align="center" prop="approver" />
      <el-table-column label="审核日期" align="center" prop="approvalDate" width="180"/>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="采购单号" align="center" prop="purchaseOrderNo" />
      <el-table-column label="采购单位" align="center" prop="purchaser" />
      <el-table-column label="物料名称" align="center" prop="materialName" />
      <el-table-column label="物料号" align="center" prop="materialNo" />
      <el-table-column label="规格型号" align="center" prop="specification" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="订单数量" align="center" prop="orderQuantity" />
      <el-table-column label="采购数量" align="center" prop="purchasedQuantity" />
      <el-table-column label="操作" width="100" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:ZcTemporaryPurchasePlan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:ZcTemporaryPurchasePlan:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!--播放语音-->
    <el-dialog title="录音播放" :visible.sync="dialogVisible" width="360px" :before-close="stop">
      <template>
        <audio
          :src="src"
          autoplay="autoplay"
          controls="controls"
          ref="audio"
        ></audio>
      </template>
    </el-dialog>
    <!-- 添加或修改临时采购计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="公司代码" prop="companyCode">
              <el-input v-model="form.companyCode" placeholder="请输入公司代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司名称" prop="companyName">
              <el-input v-model="form.companyName" placeholder="请输入公司名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采购单号" prop="purchaseOrderNo">
              <el-input v-model="form.purchaseOrderNo" placeholder="请输入采购单号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采购单位" prop="purchaser">
              <el-input v-model="form.purchaser" placeholder="请输入采购单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料名称" prop="materialName">
              <el-input v-model="form.materialName" placeholder="请输入物料名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料号" prop="materialNo">
              <el-input v-model="form.materialNo" placeholder="请输入物料号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格型号" prop="specification">
              <el-input v-model="form.specification" placeholder="请输入规格型号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单号" prop="orderNo">
              <el-input v-model="form.orderNo" placeholder="请输入订单号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单数量" prop="orderQuantity">
              <el-input v-model="form.orderQuantity" placeholder="请输入订单数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采购数量" prop="purchasedQuantity">
              <el-input v-model="form.purchasedQuantity" placeholder="请输入采购数量" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片" prop="attachment1">
              <image-upload v-model="form.attachment1"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="语音" prop="attachment2">
              <file-upload v-model="form.attachment2"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请人" prop="applicant">
              <el-input v-model="form.applicant" placeholder="请输入申请人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请日期" prop="applicationDate">
              <el-date-picker clearable
                              v-model="form.applicationDate"
                              type="date"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择申请日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核人" prop="approver">
              <el-input v-model="form.approver" placeholder="请输入审核人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核日期" prop="approvalDate">
              <el-date-picker clearable
                              v-model="form.approvalDate"
                              type="date"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择审核日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--  <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
            </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listZcTemporaryPurchasePlan, getZcTemporaryPurchasePlan, delZcTemporaryPurchasePlan, addZcTemporaryPurchasePlan, updateZcTemporaryPurchasePlan } from "@/api/business/ZcTemporaryPurchasePlan";
  import {getToken} from "@/utils/auth";
  import moment from "moment";
  export default {
    name: "ZcTemporaryPurchasePlan",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 临时采购计划表格数据
        ZcTemporaryPurchasePlanList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //播放地址
        src: "",
        dialogVisible: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          companyName: null,
          purchaseOrderNo: null,
          materialName: null,
          materialNo: null,
          specification: null,
          applicationDate: new Date(),
          orderNo: null,
        },
        upload: {
          // 是否显示弹出层
          open: false,
          // 弹出层标题
          title: "导入`ZcTemporaryPurchasePlan`信息",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/business/ZcTemporaryPurchasePlan/importData"
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          companyCode: [
            { required: true, message: "公司代码不能为空", trigger: "blur" }
          ],
          companyName: [
            { required: true, message: "公司名称不能为空", trigger: "blur" }
          ],
          purchaseOrderNo: [
            { required: true, message: "采购单号不能为空", trigger: "blur" }
          ],
          purchaser: [
            { required: true, message: "采购单位不能为空", trigger: "blur" }
          ],
          materialName: [
            { required: true, message: "物料名称不能为空", trigger: "blur" }
          ],
          materialNo: [
            { required: true, message: "物料号不能为空", trigger: "blur" }
          ],
          specification: [
            { required: true, message: "规格型号不能为空", trigger: "blur" }
          ],
          unit: [
            { required: true, message: "单位不能为空", trigger: "blur" }
          ],
          orderNo: [
            { required: true, message: "订单号不能为空", trigger: "blur" }
          ],
          orderQuantity: [
            { required: true, message: "订单数量不能为空", trigger: "blur" }
          ],
          purchasedQuantity: [
            { required: true, message: "采购数量不能为空", trigger: "blur" }
          ],
          applicant: [
            { required: true, message: "申请人不能为空", trigger: "blur" }
          ],
          applicationDate: [
            { required: true, message: "申请日期不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询临时采购计划列表 */
      getList() {
        this.loading = true;
        console.log(this.queryParams,'zzz')
        this.queryParams.applicationDate = moment.utc(this.queryParams.applicationDate).local().format('YYYY-MM-DD')
        if(this.queryParams.applicationDate == 'Invalid date'){
          this.queryParams.applicationDate = null
        }
        listZcTemporaryPurchasePlan(this.queryParams).then(response => {
          this.ZcTemporaryPurchasePlanList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          companyCode: null,
          companyName: null,
          purchaseOrderNo: null,
          purchaser: null,
          materialName: null,
          materialNo: null,
          specification: null,
          unit: null,
          orderNo: null,
          orderQuantity: null,
          purchasedQuantity: null,
          attachment1: null,
          attachment2: null,
          applicant: null,
          applicationDate: null,
          approver: null,
          approvalDate: null,
          remark: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      handlePlayMp3(row){
        this.src =process.env.VUE_APP_BASE_API+row.attachment2;
        this.play();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加临时采购计划";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getZcTemporaryPurchasePlan(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改临时采购计划";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateZcTemporaryPurchasePlan(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addZcTemporaryPurchasePlan(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除数据？').then(function() {
          return delZcTemporaryPurchasePlan(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('business/ZcTemporaryPurchasePlan/export', {
          ...this.queryParams
        }, `ZcTemporaryPurchasePlan_${new Date().getTime()}.xlsx`)
      },
      /** 导入按钮操作 */
      handleImport() {
        this.upload.title = "临时采购计划导入";
        this.upload.open = true;
      },
      /** 下载模板操作 */
      importTemplate() {
        this.download('business/ZcTemporaryPurchasePlan/importTemplate', {
        }, `临时采购计划模板.xlsx`)
      },
      /** 文件上传中处理 */
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
      },
      /** 文件上传成功处理 */
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
        this.getList();
      },
      /** 提交上传文件 */
      submitFileForm() {
        this.$refs.upload.submit();
      },
      //播放
      play() {
        this.dialogVisible = true;
        this.$refs.audio.play();
      },
      //音频暂停
      stop() {
        this.dialogVisible = false;
        this.$refs.audio.pause();
        this.$refs.audio.currentTime = 0;
      },
    }
  };
</script>
